package com.controller;

import com.czbspringmvc.annotation.*;
import com.entity.Monster;
import com.service.MonsterService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: 陈正彬
 */
@Controller
public class MonsterController {

    @AutoWired
    private MonsterService monsterService;

    //编写方法，列出名单列表
    @RequestMapping(value = "/monster/list")
    public void listMonster(HttpServletRequest req, HttpServletResponse resp) {
        resp.setContentType("text/html;charset=utf-8");

        StringBuilder content = new StringBuilder("<h1>妖怪信息列表</h1>");
        List<Monster> monsters = monsterService.listMonster();
        content.append("<table border='1px' width='500px'>");
        for (Monster monster : monsters) {
            content.append("<tr><td>" + monster.getId() +
                    "</td><td>" + monster.getName() +
                    "</td><td>" + monster.getSkill() +
                    "</td><td>" + monster.getAge() + "</td></tr>");
        }
        content.append("</table>");

        //获取Writer返回信息
        try {
            PrintWriter printWriter = resp.getWriter();
            printWriter.write(content.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //编写方法，通过name返回对应的monster集合
    @RequestMapping(value = "/monster/find")
    public void findMonsterByName(HttpServletRequest req, HttpServletResponse resp,
                                  String name) {
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("---接收到的name---" + name);

        StringBuilder content = new StringBuilder("<h1>妖怪信息列表</h1>");
        List<Monster> monsters = monsterService.findMonsterByName(name);
        content.append("<table border='1px' width='500px'>");
        for (Monster monster : monsters) {
            content.append("<tr><td>" + monster.getId() +
                    "</td><td>" + monster.getName() +
                    "</td><td>" + monster.getSkill() +
                    "</td><td>" + monster.getAge() + "</td></tr>");
        }
        content.append("</table>");

        //获取Writer返回信息
        try {
            PrintWriter printWriter = resp.getWriter();
            printWriter.write(content.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //编写处理妖怪登入的方法,返回要请求转发/重定向的字符串
    @RequestMapping("/monster/login")
    public String login(HttpServletRequest request,HttpServletResponse response,
                        @RequestParam(value = "monsterName") String mName) {
        //String mName 如果没有带@RequestParam注解 mName要和前端的名字保持一致
        //@RequestParam注解中的 value 要与前端名字一致

        System.out.println("--接收mName--" + mName);
        //将mName设置到request域
        request.setAttribute("mName",mName);
        boolean loginYN = monsterService.login(mName);
        if (loginYN) {
//            return "forward:/login_ok.jsp";
            //测试重定向
//            return "redirect:/login_ok.jsp";
            //测试默认的方式（forward）
            return "/login_ok.jsp";
            //原生的默认方式是login_ok，因为我没有做前缀和后缀的处理
            //所以有些不一样

        } else {
            return "forward:/login_error.jsp";
        }
    }
    //编写方法返回json格式的数据
    @RequestMapping("/monster/list/json")
    @ResponseBody
    public List<Monster> listMonsterByJson(HttpServletRequest request,HttpServletResponse response) {
        List<Monster> monsters = monsterService.listMonster();
        return monsters;
    }
}
